import configdata from './config'
import cache from './cache'
import httpdata from './httpApi'

module.exports = {
	
	check_login: function(callback = function() {}) {
		let token = cache.get('token');
		// token = false;
		uni.checkSession({
			success: (result) => {
				if (!token) {
					this.login(function(token) {
						callback(token);
					});
				} else {
					callback(token);
				}
			},
			fail: (result) => {
				this.login(function(token) {
					callback(token);
				});
			}
		})
	},
	login: function(callback = function() {}) {
		let loginUrl = this.config("APIHOST") + this.http("login.wxlogin");
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				uni.request({
					url: loginUrl,
					data: {
						code: loginRes.code
					},
					method: "POST",
					header: {
						'content-type': 'application/json',
					},
					success: (result) => {
						if (result.data.code != 0) {
							cache.put('token', result.data.data, 86400)
							callback(result.data.data)
						} else {
							uni.showToast({
								title: result.data.msg,
								duration: 2000,
								icon: 'none'
							})
						}
					},
				})
			}
		});
	},
	post: function(url, data) {
		url = configdata.APIHOST + url;
		let token = cache.get('token');
		// data.token = token;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					'Authorization': token, //自定义请求头信息
					// 'Authorization': "Bearer " + token, //自定义请求头信息
					'content-type': 'application/json',
				},
				success: (result) => {
					if (result.data.code == 322) {
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						});
						return;
					} else if (result.data.code == 333) { // 未实名认证
						uni.reLaunch({
							url: '/pages/login/idcard'
						})
						return;
						// cache.remove('token');
					} else if (result.data.code == 323) { // 异地登录
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						return;
						// cache.remove('token');
					}
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})

	},
	get: function(url, data, header) {
		header = header || "application/x-www-form-urlencoded";
		url = this.config("APIHOST") + url;
		let token = cache.get('token');
		// data.token = token;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					'Authorization': token,
					// 'Cookie': token,
					"content-type": header
				},
				success: function(result) {
					if (result.data.code == 322) {
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						});
						return;
					} else if (result.data.code == 333) { // 未实名认证
						uni.reLaunch({
							url: '/pages/login/idcard'
						})
						return;
						// cache.remove('token');
					} else if (result.data.code == 323) { // 异地登录
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						return;
						// cache.remove('token');
					}
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	pushFile: function(url, filePath) {
		let token = cache.get('token');
		return new Promise((succ, error) => {
			uni.uploadFile({
				url: configdata.APIHOST + url,
				filePath: filePath,
				name: 'file',
				header: {
					'Authorization': token,
					'content-type': 'multipart/form-data',
				},
				success: (result) => {
					var data = JSON.parse(result.data);
					if (data.code == 322) {
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						});
						return;
					} else if (data.code == 333) { // 未实名认证
						uni.reLaunch({
							url: '/pages/login/idcard'
						})
						return;
						// cache.remove('token');
					} else if (result.data.code == 323) { // 异地登录
						cache.remove('token');
						uni.reLaunch({
							url: '/pages/login/login'
						})
						return;
						// cache.remove('token');
					}
					succ.call(self, data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
}
